package com.dylearn.serve.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dylearn.domain.Book;
import com.dylearn.mapper.BookMapper;
import com.dylearn.serve.IBookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//这个也是可以直接继承 -------ServiceImpl<Mapper, T>
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    BookMapper bookMapper;

    @Override
    public IPage<Book> getPage(int currentPage,int pageSize) {
        IPage<Book> page = new Page<Book>(currentPage,pageSize);

        return bookMapper.selectPage(page,null);
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();

        //这个的话是直接使用条件查询 ---这里先用工具类进行空值判断
        //Strings.isNotEmpty空值判断 -> 然后限定 Book::getName 这个东西仅是 "name" -> 最后使用getName值进行查询
        lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());
        lqw.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription());

        IPage<Book> page = new Page<Book>(currentPage,pageSize);

        return bookMapper.selectPage(page,lqw);
    }
}
